home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 2 / Amiga Tools 2.iso / tools / jade / src / x11_display.c < prev    next >
C/C++ Source or Header  |  1995-03-09  |  6KB  |  248 lines

  1. /* x11_display.c -- Initialisation for X11 window-system
  2.    Copyright (C) 1993, 1994 John Harper <jsh@ukc.ac.uk>
  3.  
  4.    This file is part of Jade.
  5.  
  6.    Jade is free software; you can redistribute it and/or modify it
  7.    under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    Jade is distributed in the hope that it will be useful, but
  12.    WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with Jade; see the file COPYING.  If not, write to
  18.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "jade.h"
  21. #include "jade_protos.h"
  22.  
  23. #include <X11/Xutil.h>
  24. #include <X11/cursorfont.h>
  25.  
  26. #ifdef HAVE_UNIX
  27. # include <sys/types.h>
  28. # include <sys/time.h>
  29. # include <sys/signal.h>
  30. # include <fcntl.h>
  31. #else
  32.   you lose
  33. #endif
  34.  
  35. _PR void sys_usage(void);
  36. _PR int sys_init(int, char **);
  37.  
  38. #ifdef HAVE_UNIX
  39. _PR fd_set x11_fd_read_set;
  40. fd_set x11_fd_read_set;
  41.  
  42. _PR void (*x11_fd_read_action[])(int);
  43. void (*x11_fd_read_action[FD_SETSIZE])(int);
  44. #endif
  45.  
  46. _PR Display *x11_display;
  47. _PR int x11_screen;
  48. _PR Colormap x11_colour_map;
  49. _PR u_long x11_fore_pixel, x11_back_pixel;
  50. _PR Atom x11_wm_del_win;
  51. _PR char **x11_argv;
  52. _PR int x11_argc;
  53. _PR Cursor x11_text_cursor;
  54.  
  55. Display *x11_display;
  56. int x11_screen;
  57. Colormap x11_colour_map;
  58. u_long x11_fore_pixel, x11_back_pixel;
  59. Atom x11_wm_del_win;
  60. char **x11_argv;
  61. int x11_argc;
  62. Cursor x11_text_cursor;
  63.  
  64. static char *display_name = NULL;
  65. static char *bg_str = "white";
  66. static char *fg_str = "black";
  67. static char *geom_str = "80x24";
  68. static char *prog_name;
  69.  
  70. _PR VALUE def_font_str;
  71. VALUE def_font_str = MKSTR(DEFAULT_FONT);
  72.  
  73. static int
  74. get_resources(void)
  75. {
  76.     char *s;
  77.     if((s = XGetDefault(x11_display, prog_name, "geometry"))
  78.        || (s = XGetDefault(x11_display, "Jade", "geometry")))
  79.     geom_str = s;
  80.     if((s = XGetDefault(x11_display, prog_name, "foreground"))
  81.        || (s = XGetDefault(x11_display, "Jade", "foreground")))
  82.     fg_str = s;
  83.     if((s = XGetDefault(x11_display, prog_name, "background"))
  84.        || (s = XGetDefault(x11_display, "Jade", "background")))
  85.     bg_str = s;
  86.     if((s = XGetDefault(x11_display, prog_name, "font"))
  87.        || (s = XGetDefault(x11_display, "Jade", "font")))
  88.     def_font_str = string_dup(s);
  89.     return(TRUE);
  90. }
  91.  
  92. void
  93. sys_usage(void)
  94. {
  95.     fputs(
  96.     "where SYSTEM-OPTIONS are,\n"
  97.     "    -display DISPLAY-NAME\n"
  98.     "    -name NAME\n"
  99.     "    -geometry WINDOW-GEOMETRY\n"
  100.     "    -fg FOREGROUND-COLOUR\n"
  101.     "    -bg BACKGROUND-COLOUR\n"
  102.     "    -font FONT-NAME\n"
  103.     "    -sync\n"
  104.     , stderr);
  105. }
  106.  
  107. static int
  108. get_options(int *argc_p, char ***argv_p)
  109. {
  110.     int argc = *argc_p;
  111.     char **argv = *argv_p;
  112.     argc--;
  113.     argv++;
  114.     while((argc >= 1) && (**argv == '-'))
  115.     {
  116.     if(!strcmp("-sync", *argv))
  117.         XSynchronize(x11_display, True);
  118.     else if(argc >= 2)
  119.     {
  120.         if(!strcmp("-display", *argv))
  121.         ;
  122.         else if(!strcmp("-name", *argv))
  123.         ;
  124.         else if(!strcmp("-geometry", *argv))
  125.         geom_str = argv[1];
  126.         else if(!strcmp("-fg", *argv))
  127.         fg_str = argv[1];
  128.         else if(!strcmp("-bg", *argv))
  129.         bg_str = argv[1];
  130.         else if(!strcmp("-font", *argv))
  131.         def_font_str = string_dup(argv[1]);
  132.         else
  133.         break;
  134.         argc--; argv++;
  135.     }
  136.     else
  137.         break;
  138.     argc--; argv++;
  139.     }
  140.     *argc_p = argc;
  141.     *argv_p = argv;
  142.     return(TRUE);
  143. }
  144.  
  145. static int
  146. use_options(void)
  147. {
  148.     int x, y, w, h;
  149.     int gflgs = XParseGeometry(geom_str, &x, &y, &w, &h);
  150.     XColor tmpc;
  151.     if(gflgs & WidthValue)
  152.     def_dims[2] = w;
  153.     else
  154.     def_dims[2] = -1;
  155.     if(gflgs & HeightValue)
  156.     def_dims[3] = h;
  157.     else
  158.     def_dims[3] = -1;
  159.     /*
  160.      * need to use -ve values properly
  161.      */
  162.     if(gflgs & XValue)
  163.     def_dims[0] = x;
  164.     else
  165.     def_dims[0] = -1;
  166.     if(gflgs & YValue)
  167.     def_dims[1] = y;
  168.     else
  169.     def_dims[1] = -1;
  170.     if(!XParseColor(x11_display, x11_colour_map, fg_str, &tmpc))
  171.     fprintf(stderr, "error: invalid fg colour\n");
  172.     else if(!XAllocColor(x11_display, x11_colour_map, &tmpc))
  173.     fprintf(stderr, "error: can't allocate fg colour\n");
  174.     else
  175.     x11_fore_pixel = tmpc.pixel;
  176.     if(!XParseColor(x11_display, x11_colour_map, bg_str, &tmpc))
  177.     fprintf(stderr, "error: invalid bg colour\n");
  178.     else if(!XAllocColor(x11_display, x11_colour_map, &tmpc))
  179.     fprintf(stderr, "error: can't allocate bg colour\n");
  180.     else
  181.     x11_back_pixel = tmpc.pixel;
  182.     return(TRUE);
  183. }
  184.  
  185. static void
  186. x11_int_handler(int unused_sig)
  187. {
  188.     throw_value = int_cell;
  189.     signal(SIGINT, x11_int_handler);
  190. }
  191.  
  192. int
  193. sys_init(int argc, char **argv)
  194. {
  195.     int i;
  196.     prog_name = file_part(argv[0]);
  197.     for(i = 1; i < argc; i++)
  198.     {
  199.     /* These options have to be found *before* resources are looked
  200.        for.     */
  201.     if(!strcmp("-display", argv[i]) && (i+1 < argc))
  202.         display_name = argv[++i];
  203.     else if(!strcmp("-name", argv[i]) && (i+1 < argc))
  204.         prog_name = argv[++i];
  205.     }
  206.     x11_display = XOpenDisplay(display_name);
  207.     if(x11_display)
  208.     {
  209. #ifdef HAVE_UNIX
  210.     FD_ZERO(&x11_fd_read_set);
  211.     /* close-on-exec = TRUE     */
  212.     fcntl(ConnectionNumber(x11_display), F_SETFD, 1);
  213.     FD_SET(ConnectionNumber(x11_display), &x11_fd_read_set);
  214.  
  215.     /* Install the interrupt handler */
  216.     signal(SIGINT, x11_int_handler);
  217. #endif /* HAVE_UNIX */
  218.  
  219.     x11_screen = DefaultScreen(x11_display);
  220.     x11_colour_map = DefaultColormap(x11_display, x11_screen);
  221.     if(get_resources() && get_options(&argc, &argv) && use_options())
  222.     {
  223.         XColor fore, back;
  224.         int rc;
  225.         x11_wm_del_win = XInternAtom(x11_display, "WM_DELETE_WINDOW",
  226.                      False);
  227.         x11_text_cursor = XCreateFontCursor(x11_display, XC_xterm);
  228.         fore.pixel = x11_fore_pixel;
  229.         back.pixel = x11_back_pixel;
  230.         XQueryColor(x11_display, x11_colour_map, &fore);
  231.         XQueryColor(x11_display, x11_colour_map, &back);
  232.         XRecolorCursor(x11_display, x11_text_cursor, &fore, &back);
  233.  
  234.         /* Call the main editor setup and event loop.  */
  235.         rc = inner_main(argc, argv);
  236.  
  237.         if(x11_text_cursor)
  238.         XFreeCursor(x11_display, x11_text_cursor);
  239.         XCloseDisplay(x11_display);
  240.  
  241.         return(rc);
  242.     }
  243.     }
  244.     else
  245.     fprintf(stderr, "jade: can't open X connection\n");
  246.     return(5);
  247. }
  248.